import numpy as np
import matplotlib.pyplot as plt

def rle_to_mask(rle):
    """Compute a binary mask from an uncompressed RLE."""
    h, w = rle["size"]
    mask = np.empty(h * w, dtype=bool)
    idx = 0
    parity = False
    for count in rle["counts"]:
        mask[idx : idx + count] = parity
        idx += count
        parity ^= True
    mask = mask.reshape(w, h)
    return mask.transpose()

# 示例 RLE 数据
rle = {
    "size": (684, 1024),
    # "size": (1024, 684),
    "counts": [
    430566,
    7,
    674,
    11,
    670,
    14,
    667,
    17,
    666,
    18,
    665,
    19,
    665,
    19,
    664,
    19,
    655,
    28,
    655,
    28,
    654,
    29,
    653,
    30,
    653,
    31,
    649,
    35,
    647,
    36,
    645,
    39,
    643,
    41,
    640,
    44,
    637,
    47,
    634,
    49,
    633,
    51,
    631,
    53,
    629,
    55,
    627,
    56,
    626,
    58,
    624,
    60,
    622,
    61,
    619,
    65,
    616,
    67,
    615,
    69,
    613,
    71,
    610,
    74,
    608,
    76,
    607,
    76,
    606,
    78,
    603,
    81,
    599,
    84,
    599,
    85,
    597,
    87,
    596,
    88,
    595,
    88,
    595,
    89,
    594,
    90,
    593,
    92,
    591,
    93,
    590,
    94,
    590,
    94,
    590,
    94,
    589,
    95,
    588,
    96,
    588,
    96,
    587,
    97,
    586,
    98,
    586,
    98,
    586,
    97,
    586,
    97,
    587,
    97,
    587,
    97,
    586,
    97,
    587,
    97,
    586,
    98,
    585,
    99,
    585,
    99,
    584,
    100,
    582,
    101,
    582,
    102,
    581,
    100,
    583,
    99,
    584,
    99,
    585,
    99,
    584,
    99,
    585,
    99,
    584,
    99,
    585,
    99,
    585,
    99,
    584,
    101,
    583,
    101,
    583,
    101,
    582,
    101,
    583,
    101,
    582,
    101,
    582,
    102,
    582,
    102,
    581,
    102,
    582,
    102,
    581,
    103,
    581,
    103,
    581,
    102,
    581,
    103,
    581,
    103,
    581,
    102,
    582,
    102,
    582,
    17,
    6,
    78,
    583,
    17,
    6,
    78,
    583,
    17,
    4,
    80,
    583,
    18,
    1,
    82,
    582,
    102,
    582,
    101,
    583,
    101,
    582,
    46,
    2,
    53,
    583,
    27,
    22,
    52,
    582,
    28,
    22,
    52,
    582,
    28,
    4,
    2,
    16,
    52,
    582,
    28,
    3,
    6,
    1,
    7,
    4,
    52,
    582,
    29,
    2,
    71,
    582,
    29,
    1,
    71,
    582,
    100,
    584,
    100,
    584,
    65,
    3,
    31,
    584,
    56,
    2,
    8,
    3,
    31,
    584,
    56,
    1,
    42,
    585,
    56,
    1,
    42,
    584,
    57,
    1,
    25,
    2,
    14,
    585,
    57,
    1,
    25,
    2,
    14,
    585,
    57,
    1,
    40,
    585,
    58,
    1,
    40,
    585,
    58,
    1,
    40,
    584,
    59,
    1,
    39,
    585,
    99,
    584,
    100,
    584,
    66,
    9,
    24,
    584,
    69,
    6,
    25,
    584,
    99,
    585,
    99,
    584,
    100,
    584,
    100,
    583,
    101,
    583,
    101,
    582,
    102,
    582,
    101,
    582,
    102,
    582,
    102,
    581,
    103,
    581,
    61,
    2,
    39,
    582,
    60,
    3,
    39,
    581,
    61,
    3,
    39,
    581,
    60,
    4,
    38,
    582,
    60,
    4,
    38,
    582,
    62,
    1,
    38,
    582,
    102,
    582,
    102,
    582,
    102,
    581,
    103,
    580,
    103,
    580,
    104,
    580,
    104,
    580,
    103,
    581,
    103,
    580,
    61,
    6,
    35,
    582,
    48,
    1,
    11,
    8,
    33,
    582,
    64,
    5,
    32,
    583,
    101,
    582,
    101,
    583,
    101,
    582,
    101,
    583,
    101,
    583,
    101,
    583,
    101,
    582,
    102,
    582,
    101,
    582,
    102,
    582,
    101,
    582,
    102,
    582,
    102,
    581,
    102,
    582,
    102,
    581,
    102,
    582,
    102,
    581,
    103,
    581,
    102,
    582,
    102,
    581,
    103,
    581,
    103,
    581,
    103,
    581,
    103,
    580,
    103,
    581,
    103,
    580,
    104,
    580,
    103,
    581,
    102,
    582,
    102,
    581,
    102,
    582,
    102,
    581,
    103,
    581,
    102,
    582,
    102,
    581,
    102,
    582,
    101,
    582,
    101,
    583,
    101,
    582,
    101,
    583,
    101,
    583,
    100,
    583,
    100,
    584,
    99,
    585,
    99,
    584,
    99,
    585,
    99,
    584,
    100,
    584,
    99,
    584,
    100,
    584,
    100,
    584,
    99,
    585,
    99,
    585,
    99,
    584,
    100,
    584,
    99,
    585,
    99,
    584,
    100,
    584,
    99,
    584,
    100,
    584,
    99,
    584,
    99,
    585,
    98,
    585,
    98,
    586,
    94,
    590,
    94,
    589,
    94,
    590,
    94,
    590,
    94,
    590,
    94,
    589,
    95,
    589,
    94,
    589,
    95,
    589,
    94,
    589,
    95,
    589,
    94,
    590,
    93,
    590,
    94,
    590,
    93,
    590,
    94,
    590,
    94,
    589,
    94,
    590,
    94,
    589,
    94,
    590,
    94,
    590,
    93,
    591,
    93,
    590,
    93,
    591,
    93,
    591,
    92,
    591,
    93,
    591,
    93,
    590,
    93,
    590,
    94,
    590,
    93,
    591,
    93,
    590,
    94,
    589,
    94,
    590,
    94,
    590,
    93,
    591,
    92,
    591,
    93,
    591,
    92,
    592,
    92,
    591,
    93,
    591,
    93,
    591,
    92,
    591,
    93,
    591,
    93,
    591,
    92,
    591,
    93,
    591,
    93,
    591,
    92,
    591,
    93,
    591,
    93,
    591,
    92,
    591,
    93,
    591,
    92,
    592,
    91,
    592,
    92,
    592,
    91,
    593,
    91,
    592,
    91,
    593,
    91,
    593,
    91,
    592,
    91,
    593,
    91,
    593,
    91,
    592,
    91,
    593,
    91,
    593,
    90,
    593,
    91,
    593,
    90,
    594,
    89,
    594,
    89,
    595,
    87,
    597,
    86,
    597,
    86,
    598,
    83,
    600,
    81,
    603,
    78,
    605,
    75,
    608,
    73,
    611,
    70,
    614,
    68,
    615,
    65,
    619,
    62,
    622,
    59,
    625,
    57,
    626,
    54,
    630,
    51,
    633,
    48,
    635,
    45,
    639,
    42,
    641,
    41,
    643,
    38,
    645,
    36,
    648,
    33,
    651,
    30,
    653,
    29,
    655,
    27,
    657,
    24,
    660,
    21,
    663,
    17,
    668,
    14,
    670,
    11,
    674,
    8,
    678,
    3,
    52537
]
}

# 将 RLE 转换为掩码
mask = rle_to_mask(rle)

# 可视化掩码
plt.figure(figsize=(8, 8))
plt.imshow(mask, cmap="gray")
plt.title("Mask from RLE")
plt.axis("off")
plt.show()
